package com.abbas.myviewtest.activity.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.gyf.immersionbar.ImmersionBar;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * activity基类
 */

public abstract class BaseActivity extends RxAppCompatActivity {

    protected Unbinder unBinder;
    protected Context mContext;


    /**
     * 状态栏沉浸
     */
    private ImmersionBar mImmersionBar;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setWindowParams();

        setContentView(getLayoutId());
        mContext = this;
        unBinder = ButterKnife.bind(this);
        // 初始化沉浸式状态栏
//        if (isStatusBarEnabled()) {
        getStatusBarConfig().init();
//        }
        init();
        try {
            EventBus.getDefault().register(this);
        } catch (Exception e) {

        }
    }

    public void setWindowParams() {

    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onRestart() {
        super.onRestart();

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //移除view绑定
        if (unBinder != null) {
            unBinder.unbind();
        }
        try {
            EventBus.getDefault().unregister(this);
        } catch (Exception e) {

        }

    }

    public Context getViewContext() {
        return mContext;
    }

    protected abstract int getLayoutId();

    protected abstract void init();


    /**
     * 是否使用沉浸式状态栏
     */
    protected boolean isStatusBarEnabled() {
        return false;
    }

    /**
     * 状态栏字体深色模式
     */
    protected boolean isStatusBarDarkFont() {
        return true;
    }

    /**
     * 初始化沉浸式状态栏
     */
    @NonNull
    protected ImmersionBar createStatusBarConfig() {
        return ImmersionBar.with(this)
                // 默认状态栏字体颜色为黑色
                .statusBarDarkFont(isStatusBarDarkFont());
    }

    /**
     * 获取状态栏沉浸的配置对象
     */
    @NonNull
    public ImmersionBar getStatusBarConfig() {
        if (mImmersionBar == null) {
            mImmersionBar = createStatusBarConfig();
        }
        return mImmersionBar;
    }

    protected void showToast(String message) {
        Toast.makeText(BaseActivity.this, message, Toast.LENGTH_SHORT).show();
    }

    protected void migrateTo(Class clazz) {
        startActivity(new Intent(this, clazz));
    }

}
